import requests


headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0",
    "sec-ch-ua-platform": "\"Windows\"",
    "Referer": "https://www.pexels.com/zh-cn/",
    "sec-ch-ua": "\"Microsoft Edge\";v=\"137\", \"Chromium\";v=\"137\", \"Not/A)Brand\";v=\"24\"",
    "sec-ch-ua-mobile": "?0",
    "X-Forwarded-CF-IPRegionCode;": "",
    "traceparent": "00-0000000000000000caac9c656015c870-106a58dbb80fbe5d-01",
    "secret-key": "H2jk9uKnhRmL6WPwh89zBezWvr",
    "X-Client-Type": "react",
    "Content-Type": "application/json",
    "X-Forwarded-CF-Connecting-IP;": "",
    "X-Forwarded-HTTP_CF_IPCOUNTRY;": ""
}
url = "https://www.pexels.com/zh-cn/api/v2/feed"
params = {
    "seed": "2025-06-18T18:00:05.392Z",
    "per_page": "12",
    "seo_tags": "true"
}
start = 0
stop_flag = False
while not stop_flag:
    response = requests.get(url, headers=headers, params=params)
    data = response.json()
    result = data["data"]

    for temp in result:

        if start >= 24:
            stop_flag = True
            break
        if temp["type"] == "video":
            download_link = temp["attributes"]["video"]["download_link"]
            print("视频开始写入")
            with open(f"E:\\cherrytree\\video\\{str(temp["id"])}.mp4", "wb") as f:
                f.write(requests.get(download_link).content)
            print("视频写入成功")

        if temp["type"] == "photo":
            download_link = temp["attributes"]["image"]["download_link"]
            print("图片开始写入")
            with open(f"E:\\cherrytree\\picture\\{str(temp["id"])}.jpg", "wb") as f:
                f.write(requests.get(download_link).content)
            print("图片写入成功")
        start += 1
        print(f"成功存入{start}次")
        print(f"本次cursor为:{params["seed"]}")

    # 更新下一次的params["seed"]
    params["seed"] = data["pagination"]["cursor"]


